﻿using MeetingRoomBooking.Application.Common.Interfaces;
using MeetingRoomBooking.Domain.Events;

namespace MeetingRoomBooking.Application.Rooms.Commands;
public class DeleteRoomCommand:IRequest
{

    public int Id { get; set; }
}

public class DeleteRoomCommandHander : IRequestHandler<DeleteRoomCommand>
{
    private readonly IApplicationDbContext _context;  

    public DeleteRoomCommandHander(IApplicationDbContext context)
    {
        _context = context;     
    }    

    public async Task Handle(DeleteRoomCommand request, CancellationToken cancellationToken)
    {
        var room = await _context.Rooms.FindAsync(new object[] { request.Id });
       
        if (room == null) throw new Exception($"Room Id:{request.Id} not found.");

        _context.Rooms.Remove(room);

        //room.AddDomainEvent(new RoomCreatedEvent(room));        

        await _context.SaveChangesAsync(cancellationToken);          
    }
}
